package tcln.gxtx.client.widget.grid.renderer;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;

/**
 * Abstract grid cell renderer for a treegrid.
 * 
 * @param <M>
 *            tree model type.
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public abstract class AbstractGridCellRenderer<M extends ModelData> implements GridCellRenderer<M> {

    /**
     * {@inheritDoc}
     */
    @Override
    public Object render(M model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<M> store,
            Grid<M> grid) {
        config.css = ""; // clear old CSS for new row
        Object r = formatModelData(model, property);
        applyStandardStyles(model, property, config, rowIndex, colIndex, store, grid);
        return r;
    }

    /**
     * Applies all standard styles for standard column data types. For example, numeric value should be right-aligned.
     * 
     * @param model
     * @param property
     * @param config
     * @param rowIndex
     * @param colIndex
     * @param store
     * @param grid
     */
    protected void applyStandardStyles(M model, String property, ColumnData config, int rowIndex, int colIndex,
            ListStore<M> store, Grid<M> grid) {
        // nothing to do by default
    }

    /**
     * Formats data before rendering the text portion in a cell.
     * 
     * @param model
     * @param property
     * @return Formatted data as string.
     */
    protected abstract String formatModelData(M model, String property);
}
